using System;
using System.Collections.Generic;
using LogSystem;
using UnityEngine;
using XLua;
using Yoozoo.Framework.Managers;

namespace Yoozoo.Gameplay.City
{

    public class CityEventManager: MonoBehaviour
    {
        private CityTouchManager _touchManager;
        private RoomDataManager _dataManager;

        public Action<RoomData, RaycastHit> onClickRoom;
        public const string LUAFunctionName_OnClickRoom = "L_CityEventMgr.onClickRoom";
        public LuaFunction luaOnClickRoom; 
        
        public const string LUAFunctionName_OnClickIndustry = "L_CityEventMgr.onClickIndustry";
        public LuaFunction luaOnClickIndustry;
        
        public const string LUAFunctionName_OnClickSpace = "L_CityEventMgr.onClickSpace";
        public LuaFunction luaOnClickSpace; 
        
        public const string LUAFunctionName_OnDragStart = "L_CityEventMgr.onDragStart";
        public LuaFunction luaOnDragStart; 
        
        public const string LUAFunctionName_OnClickScreen= "L_CityEventMgr.onClickScreen";
        public LuaFunction luaOnClickScreen; 
        
        public const string LUAFunctionName_OnGmCmd = "L_CityEventMgr.onGmCmd";
        public LuaFunction luaOnGmCmd; 
        
        public const string LUAFunctionName_OnLODLevelChange = "L_CityEventMgr.onLODLevelChange";
        public LuaFunction luaOnLODLevelChange;
        public const string LUAFunctionName_OnNextLODPercentUpdate = "L_CityEventMgr.onLODPercentUpdate";
        public LuaFunction luaOnNextLODPercentUpdate;
        public const string LUAFunctionName_OnClickNoRoom = "L_CityEventMgr.onClickNoRoom";
        public LuaFunction luaOnClickNoRoom;

        public const string LUAFunctionName_OnPinchStart = "L_CityEventMgr.onPinchStart";
        public LuaFunction luaOnPinchStart;

        // key：房间ID
        private Dictionary<int, Action<RaycastHit>> ClickEventMap = new Dictionary<int, Action<RaycastHit>>();

        public void Init(CityTouchManager touchManager,RoomDataManager dataManager)
        {
            _touchManager = touchManager;
            _dataManager = dataManager;

            if (CityGlobalValue.IS_RUNTIME_MODE)
            {
                luaOnClickRoom = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnClickRoom);
                luaOnClickIndustry = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnClickIndustry);
                luaOnClickSpace = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnClickSpace);
                luaOnDragStart = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnDragStart);
                luaOnClickScreen = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnClickScreen);
                luaOnLODLevelChange = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnLODLevelChange);
                luaOnNextLODPercentUpdate = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnNextLODPercentUpdate);
                luaOnGmCmd = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnGmCmd);
                luaOnClickNoRoom = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnClickNoRoom);
                luaOnPinchStart = LuaManager.mainState.Global.GetInPath<LuaFunction>(LUAFunctionName_OnPinchStart);
            }

            _touchManager.onClick += OnClick;
            _touchManager.onDragStart += OnDragStart;
            _touchManager.onClickScreen += OnClickScreen;
            _touchManager.onPinchStart += OnPinchStart;
        }



        public void AddClickEvent(int rID, Action<RaycastHit> callBack)
        {
            Action<RaycastHit> action;
            if (!ClickEventMap.TryGetValue(rID,out action))
            {
                ClickEventMap[rID] = callBack;
                return;
            }
            action += callBack;
        }

        public void RemoveClickEvent(int rID, Action<RaycastHit> callBack)
        {
            Action<RaycastHit> action;
            if (ClickEventMap.TryGetValue(rID, out action))
            {
                action -= callBack;
            }
        }
        
        private void OnDragStart(Vector3 pos, bool islongtap, bool isonui)
        {
            luaOnDragStart?.Action(pos,islongtap,isonui);   
        }
        
        private void OnClickScreen(Vector3 clickposition, bool isdoubleclick, bool islongtap, bool isonui)
        {
            luaOnClickScreen?.Action(clickposition,isdoubleclick,islongtap,isonui);
        }

        private void OnPinchStart(Vector3 pinchCenter, float pinchDistance)
		{
            luaOnPinchStart?.Action(pinchCenter, pinchDistance);

        }
        private void OnClick(int count, RaycastHit[] hits,bool isOnUi)
        {
            if (isOnUi) return;
            
            float minDistance = float.MaxValue;
            int minDisIndex = -1;
            for (int i = 0; i < count; i++)
            {
                if (hits[i].collider != null && hits[i].distance < minDistance)
                {
                    minDistance = hits[i].distance;
                    minDisIndex = i;
                }
            }
            var hit = hits[minDisIndex];

            if (hit.transform.name == "CITY_SPACE")
            {
                if (CityGlobalValue.IS_RUNTIME_MODE)
                {
                    luaOnClickSpace?.Call();
                    luaOnClickNoRoom?.Call();
                }
                return;
            }
            
            // 点击产业检测
            CityIndustryObject cityIndustryObject = hit.transform.GetComponent<CityIndustryObject>();
            if (cityIndustryObject)
            {
                Debug.Log(LogModule.CitySimulation,$"点击到产业,ID: {cityIndustryObject.IndustryId},位置：{cityIndustryObject.IndPosId}");
                luaOnClickIndustry?.Action(cityIndustryObject.IndustryId,cityIndustryObject.IndPosId);
                return;
            }

            // 点击房间检测
            RoomData rData = _dataManager.GetRoomData(hit.collider);
            if (rData)
            {
                Debug.Log(LogModule.CityRoom, $"点击到房间{rData.rID}");
                onClickRoom?.Invoke(rData, hit);
                if (CityGlobalValue.IS_RUNTIME_MODE)
                {
                    luaOnClickRoom?.Action(rData, hit);
                }
                if (ClickEventMap.ContainsKey(rData.rID))
                {
                    ClickEventMap[rData.rID]?.Invoke(hit);
                }

                return;
            }
            if (CityGlobalValue.IS_RUNTIME_MODE)
            {
                luaOnClickNoRoom?.Call();
            }

        }

        public void OnLodLevelChanged(int cur, int prev)
        {
            luaOnLODLevelChange?.Action(cur,prev);
        }

        public void OnLodPercentChanged(int lod,float cameraHeight, float cur, float prev)
        {
            luaOnNextLODPercentUpdate?.Action(lod, cameraHeight, cur, prev);
        }
        
        public void OnGmCmd(string cmd)
        {
            luaOnGmCmd?.Action(cmd);
        }
        
        public void OnDispose()
        {
            _touchManager.onClick-= OnClick;
        }
        
    }
}